package com.qhs.framework.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qhs.sys.exception.JsonException;

import java.util.List;
import java.util.Map;

/**
 *  json 转化工具类
 *
 */
public class JsonUtil {
    private static ObjectMapper objectMapper = null;

    static {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();

            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
            SimpleModule simpleModule = new SimpleModule();
//            simpleModule.addSerializer(new StringUnicodeSerializer());

            simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
            simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

            simpleModule.addSerializer(Integer.class, ToStringSerializer.instance);
            simpleModule.addSerializer(Integer.TYPE, ToStringSerializer.instance);
            objectMapper.registerModule(simpleModule);
           // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }
    }

    /**
     * 对象转换成JSON字符串
     *
     * @param obj 需要转换的对象
     * @return 对象的string字符
     */
    public static String toJson(Object obj) throws JsonException {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }

    public static <T> T converObject(Object obj, Class<T> type) throws JsonException {
        try {
            return objectMapper.convertValue(obj,type);
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }

    /**
     * JSON字符串转换成对象
     *
     * @param jsonString 需要转换的字符串
     * @param type       需要转换的对象类型
     * @return 对象
     */
    public static <T> T fromJson(String jsonString, Class<T> type) throws JsonException {
        try {
            return objectMapper.readValue(jsonString, type);
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }


    /**
     * 将json字符串转换成map对象
     *
     * @param json
     * @return 哈希表
     */
    public static Map<String, Object> jsonToMap(String json) throws JsonException {
        try {
            return objectMapper.readValue(json, Map.class);
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }

    /**
     * 将json字符串转换成list对象
     *
     * @param json
     * @return 哈希表
     */
    public static List jsonToList(String json) throws JsonException {
        try {
            return objectMapper.readValue(json, List.class);
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }

    /**
     * 将json字符串转换成map对象
     *
     * @param json
     * @return 哈希表
     */
    public static Object[] jsonToArray(String json) throws JsonException {
        try {
            return objectMapper.readValue(json, Object[].class);
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }

    /**
     * 将json字符串转换成map对象
     *
     * @param json
     * @return
     */
    public static long[] jsonToLongArray(String json) throws JsonException {
        try {
            String[] ss = objectMapper.readValue(json, String[].class);
            long[] ls = new long[ss.length];
            for (int i = 0, len = ss.length; i < len; i++) {
                ls[i] = Long.parseLong(ss[i]);
            }
            return ls;
        } catch (Exception e) {
            throw new JsonException(e);
        }
    }
}
